package com.fenghongzhang.music.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenghongzhang.music.MainActivity;
import com.fenghongzhang.music.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelcomeFragment extends Fragment {
    private ImageView three;
    private TextView time;
    private Button btn;

    private Timer timer;

    //倒计时五秒
    private int count = 5;

    //倒计时
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);


            //什么时候开始?
            if(msg.what == 100){
                //倒计时开始
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(101);
                    }
                },0,1000);

            }else if (msg.what == 101){
                count--;
                time.setText("倒计时:"+count+"秒");

                if(count == 0){
                    timer.cancel();
                    //结束并跳转()
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);

                    //结束页面
                    getActivity().finish();
                }




            }





        }
    };

    public WelcomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_welcome, container, false);


        three = (ImageView) inflate.findViewById(R.id.three);
        time = (TextView) inflate.findViewById(R.id.time);
        btn = (Button) inflate.findViewById(R.id.btn);

        //点击btm跳转到首页
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                timer.cancel();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);

                //把引导的页面结束掉
                getActivity().finish();

            }
        });

        return inflate;
    }

}
